package com.demo;

import java.util.*;

/**
 * 方法说明: 整算法的类
 * @return
 * @version v1.0
 * @author 梁阿龙
 * @date 2022-09-14 12:41*/
public class algorithm {
    public static void main(String[] args) {

        System.out.println(niuBi("IV"));
    }


    public static String longestCommonPrefix(String[] strs) {
        ArrayList<String> strings = new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();
        ArrayList<char[]> chars = new ArrayList<>();
        for (String s: strs) {
            strings.add(s);
        }
        for (int i = 0; i <strings.size(); i++) {
            strings.get(i).toCharArray();
            
        }

        return  "";
    }

    /**
     * 方法说明: 力扣第十九题 -- 罗马数字转整数
     *          * 需要对罗马数字有一定的了解 *
     * @param s
     * @return int
     * @version v1.0
     * @author 梁阿龙[liangalong@fescotech.com]
     * @date 2022-10-30 0:01
    */
    public static int romanToInt(String s) {
        s = s.replace("IV","a");
        s = s.replace("IX","b");
        s = s.replace("XL","c");
        s = s.replace("XC","d");
        s = s.replace("CD","e");
        s = s.replace("CM","f");

        int i = 0;
        for (int j = 0; j < s.length(); j++) {
            i += getValue(s.charAt(i));
        }
        return i;
    }

    public static int getValue(char c) {
        switch(c) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            case 'a': return 4;
            case 'b': return 9;
            case 'c': return 40;
            case 'd': return 90;
            case 'e': return 400;
            case 'f': return 900;
        }
        return 0;
    }
    /**
     * 方法说明: 力扣第十九题 -- 罗马数字转整数 * 大佬的解法 *
     * @param s
     * @return int
     * @version v1.0
     * @author 梁阿龙[liangalong@fescotech.com]
     * @date 2022-10-30 0:08
    */
    public static int niuBi(String s){
        // 先获得int array
        int length = s.length();
        int[] nums = new int[length];
        int num = 0;
        for (int i = 0; i < length; i++) {
            switch (s.charAt(i)) {
                case 'M':
                    num = 1000;
                    break;
                case 'D':
                    num = 500;
                    break;
                case 'C':
                    num = 100;
                    break;
                case 'L':
                    num = 50;
                    break;
                case 'X':
                    num = 10;
                    break;
                case 'V':
                    num = 5;
                    break;
                case 'I':
                    num = 1;
                    break;
            }
            nums[i] = num;
        }

        // 若较小的数字在较大的数字前面就加负号
        int rtn = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i < nums.length - 1 && nums[i] < nums[i+1]) {
                nums[i] = - nums[i];
            }
            rtn += nums[i];
        }
        return rtn;
    }


    /**
     * 方法说明:力扣第九题 -- 回文字
     * @param x
     * @return boolean
     * @version v1.0
     * @author 梁阿龙[liangalong@fescotech.com]
     * @date 2022-10-29 23:30
    */
    public static boolean isPalindrome1(int x) {
        char[] chars = String.valueOf(x).toCharArray();
        ArrayList<String> list = new ArrayList<>();
        int j = 0;
        for (int i = chars.length; i > 0 ; i--) {
            list.add(String.valueOf(chars[i-1]));
        }
        String s = list.toString();
        String s1 = Arrays.toString(chars);
        if (s.equals(s1)) {
            return true;
        }

        return false;
    }
    /**
     * 方法说明:力扣第九题 -- 回文字
     * @param x
     * @return boolean
     * @version v1.0
     * @author 梁阿龙[liangalong@fescotech.com]
     * @date 2022-10-29 23:30
    */
    public static boolean isPalindrome(int x){
        return new StringBuilder(String.valueOf(x)).reverse().toString().equals(String.valueOf(x));
    }

    /**
     * 方法说明: 力扣第一题 -- 两数之和
     *         int[] nums = new int[4];
     *         nums[0] = 2;
     *         nums[1] = 7;
     *         nums[2] = 11;
     *         nums[3] = 5;
     *         int[] ints = twoSum(nums, 9);
     *         System.out.println(ints.toString());
     * @param nums
     * @param target
     * @return int[]
     * @version v1.0
     * @author 梁阿龙[liangalong@fescotech.com]
     * @date 2022-10-29 22:21
    */
    public static int[] twoSum(int[] nums, int target) {
        int[] ints = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if (nums[i]+nums[j]==target){
                    ints[0] = i;
                    ints[1] = j;
                    return ints;
                }
            }
        }
        return ints;
    }

}
